package medium;

import java.util.ArrayList;
import java.util.List;
/*
114. 二叉树展开为链表
解题思路：先通过前序遍历获得节点访问顺序，再依次修改每个节点的指向
 */
public class LeeCode114 {

  public static void main(String[] args) {

  }

  public void flatten(TreeNode root) {
    ArrayList<TreeNode> list = new ArrayList<>();
    preorderTraversal(root,list);
    int size = list.size();
    for (int i = 1; i < size; i++) {
      TreeNode prev = list.get(i - 1);
      TreeNode cur = list.get(i);
      prev.left = null;
      prev.right = cur;
    }
  }

  public void preorderTraversal(TreeNode root, List<TreeNode> list){
    if (root != null){
      list.add(root);
      preorderTraversal(root.left,list);
      preorderTraversal(root.right,list);
    }
  }

  public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
  }
}
